using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace  GameManager.GamePlay
{
    public class PowerValueImage : MonoBehaviour,IPointerDownHandler,IPointerUpHandler,IDragHandler
    {
        public Canvas canvas;
        public RectTransform EndPostion;

        public Image DragImage;

        public float MaxPosition;
        
        [Header("力量系数")]
        public float Power;
        
        public bool isMouseDown;

        [Header("鼠标点击的初始位置")]
        private Vector2 initPos;
        
        [Header("鼠标持续按下的当前位置")]
        private Vector2 currentPos;
        
        [Header("球杆图片的初始位置")]
        private Vector2 ImageInitPos;

        [Header("图片能拉取的总值")]

        private float MaxImageDragValue;
        
        private RectTransform rect;

        public Action<float> PowerDragAction;
        public Action<float> PowerUpAction;
        public Action PowerDownAction;

        public Image TutoriseImage;
        
        private void Start()
        {
            isMouseDown = false;
            initPos = Vector3.zero;
            rect = GetComponent<RectTransform>();
            
            #if UNITY_WEBGL && !UNITY_EDITOR
            AddGlobalMouseHandler();
            #endif
        }

        public void OnPointerDown(PointerEventData eventData)
        {
            // ImageInitPos = rect.anchoredPosition;
            Power = 0;
            initPos = Vector3.zero;
            currentPos = Vector3.zero;
            DragImage.rectTransform.anchoredPosition = initPos;
                ImageInitPos = DragImage.rectTransform.anchoredPosition;
            isMouseDown = true;
            // RectTransformUtility.ScreenPointToLocalPointInRectangle
            //     (rect.parent.GetComponent<RectTransform>(), eventData.position, eventData.pressEventCamera, out initPos);
            RectTransformUtility.ScreenPointToLocalPointInRectangle
                (DragImage.rectTransform.parent.GetComponent<RectTransform>(), eventData.position, eventData.pressEventCamera, out initPos);
            MaxImageDragValue = ImageInitPos.y - EndPostion.anchoredPosition.y; //球杆图片初始位置减最大拉去值，就是球杆的拉去的总值
            PowerDownAction?.Invoke();

            if (TutoriseImage.gameObject.activeSelf) {
                TutoriseImage.gameObject.SetActive(false);
            }
        }

        // void Update()
        // {
            // if (isMouseDown) {
            //     Debug.Log(Input.mousePosition);
            //     if (Input.mousePosition.x < 0 || Input.mousePosition.x > Screen.width || Input.mousePosition.y < 0 || Input.mousePosition.y > Screen.height) {
            //         isMouseDown = false;
            //         initPos = Vector3.zero;
            //         currentPos = Vector3.zero;
            //         // rect.anchoredPosition = ImageInitPos;
            //         DragImage.rectTransform.anchoredPosition = ImageInitPos;
            //         if (Power != 0)
            //         {
            //             PowerUpAction?.Invoke(Power);
            //         }
            //     }
            // }
        // }

        public void OnPointerUp(PointerEventData eventData)
        {
            // #if !UNITY_WEBGL || UNITY_EDITOR
            isMouseDown = false;
            initPos = Vector3.zero;
            currentPos = Vector3.zero;
            // rect.anchoredPosition = ImageInitPos;
            DragImage.rectTransform.anchoredPosition = ImageInitPos;
            if (Power != 0)
            {
                PowerUpAction?.Invoke(Power);
            }
            // #endif
        }

        public void OnDrag(PointerEventData eventData)
        {

            if (isMouseDown)
            {
                // 获取鼠标位置，unity中左下角为（0,0），右上角为（1,1）

                Vector2 mousePosition = eventData.position;
                //将鼠标的位置转化为canvas的坐标系统
                // RectTransformUtility.ScreenPointToLocalPointInRectangle(rect.parent.GetComponent<RectTransform>(), mousePosition, eventData.pressEventCamera, out currentPos);
                RectTransformUtility.ScreenPointToLocalPointInRectangle(DragImage.rectTransform.parent.GetComponent<RectTransform>(), mousePosition, eventData.pressEventCamera, out currentPos);


                // if (rect.anchoredPosition.y <= ImageInitPos.y)
                if (DragImage.rectTransform.anchoredPosition.y <= ImageInitPos.y)
                {
                    // if (rect.anchoredPosition.y > EndPostion.anchoredPosition.y)
                    // {
                    //     
                    // }
                    // 将鼠标位置设置给UI元素
                    // rect.anchoredPosition =
                    //     new Vector2(rect.anchoredPosition.x, ImageInitPos.y - (initPos.y - currentPos.y));\
                    DragImage.rectTransform.anchoredPosition =
                        new Vector2(DragImage.rectTransform.anchoredPosition.x, ImageInitPos.y - (initPos.y - currentPos.y));
                    // Debug.Log(rect.anchoredPosition.y + ":" + ImageInitPos.y);
                    // if (rect.anchoredPosition.y > ImageInitPos.y)
                    // {
                    //     rect.anchoredPosition = ImageInitPos;
                    // }
                    if (DragImage.rectTransform.anchoredPosition.y > ImageInitPos.y)
                    {
                        DragImage.rectTransform.anchoredPosition = ImageInitPos;
                    }


                    // if (rect.anchoredPosition.y <= EndPostion.anchoredPosition.y)
                    // {
                    //     rect.anchoredPosition = EndPostion.anchoredPosition;
                    // }
                    if (DragImage.rectTransform.anchoredPosition.y <= EndPostion.anchoredPosition.y)
                    {
                        DragImage.rectTransform.anchoredPosition = EndPostion.anchoredPosition;
                    }
                }

                // Power = (ImageInitPos.y - rect.anchoredPosition.y) / (ImageInitPos.y - EndPostion.anchoredPosition.y) * BiliardGameManager.instance.PowerFactor;
                Power = (ImageInitPos.y - DragImage.rectTransform.anchoredPosition.y) / (ImageInitPos.y - EndPostion.anchoredPosition.y) * BiliardGameManager.instance.PowerFactor;

                PowerDragAction?.Invoke(Power);
            }

        }
    }

}
